import axios from 'axios'
import { Message, Loading } from 'element-ui'
import { removeToken } from '@/utils/auth'
import router from '@/router'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_PREFIX, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  crossDomain: true,
  timeout: 50000 // request timeout
})

let requestLoading

const loadList = ['530012', '530058']
let urlc
// request interceptor
service.interceptors.request.use(
  (config) => {
    // 获取接口 c 参数
    urlc = config.url.split('do?c=')[1]
    // debugger
    // headers 不需要 场馆ID gymId 的接口
    const noGymId = [
      '1001',
      '1002',
      '1003',
      '1004',
      '1005',
      '1006',
      '1007',
      '1008',
      '1009',
      '1010',
      '1011',
      '1012',
      '1013',
      '1014',
      '1015',
      '1016',
      '1017',
      '1018',
      '1019',
      '1020',
      '1021',
      '1022',
      '1023',
      '1024',
      '1026'
    ]
    // 加载
    if (loadList.includes(urlc)) {
      requestLoading = Loading.service()
    }
    // requestLoading = Loading.service()
    // 商户ID
    config.headers.version = 'DD_2.1.5'
    config.headers.shopId = localStorage.getItem('shopId') || 1
    // 判断接口是否属于 不需要 场馆ID gymId 的接口
    if (!noGymId.includes(urlc)) {
      config.headers.gymId = sessionStorage.getItem('venueId')
    }
    // 临时参数
    // config.url = config.url + '&isTest=1'
    return config
  },
  (error) => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    const res = response.data
    if (loadList.includes(urlc)) {
      requestLoading.close()
    }
    // console.log('head', urlc)
    // console.log('res', res)
    // eslint-disable-next-line eqeqeq
    if (res.errorCode !== '000000' && res.success != true) {
      Message({
        message: res.errorMsg || 'Error',
        type: res.errorCode === '90104' ? 'warning' : 'error',
        duration: 5 * 1000
      })
      if (res.errorCode === '00016' || res.errorCode === '00068') {
        const shopId = localStorage.getItem('shopId') || ''
        removeToken()
        sessionStorage.clear()
        localStorage.clear()
        localStorage.setItem('shopId', shopId)
        router.replace(`/login`)
      }
      return Promise.reject(new Error(res.errorMsg || 'Error'))
    } else {
      return Promise.resolve(res)
    }
  },
  (error) => {
    // console.log('err' + error) // for debug
    if (loadList.includes(urlc)) {
      requestLoading.close()
    }
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
